/**
 * 
 */
package com.ms.service.module.gamelog.db;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.Jedis;

import com.ms.service.db.jedis.JedisManager;
import com.xpec.c4.db.model.log.basic.BasicLogModel;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.game.utility.JsonConvert;
import com.xpec.c4.service.module.ModuleName;

/**
 * @author TW15010
 *
 */
public class GameLogDAO {
	/** log4j object */
	private static final Log log = LogFactory.getLog(GameLogDAO.class);

	/** 取得 redis 資源的 module */
	public static final String moduleName = ModuleName.GAMELOG;
	
	/** 百家樂連勝TEMP放在REDIS裡面用的前綴KEY字串 */
	public static final String COMBOS_RECORD_FOR_REBOOT = "bacr_combo:";

	/**
	 * 將指定的 log model 存到 redis 中
	 * 
	 * @param key
	 *            儲存的 key
	 * @param logModel
	 *            model 實體
	 * @return 成功 true<br>
	 *         失敗 false
	 */
	public static boolean saveLogModelToRedis(String key, BasicLogModel logModel) {
		if (!GameUtility.isHaveString(key)) {
			if (log.isErrorEnabled()) {
				GameUtility.logErrorException(log, "key=" + key);
			}
			return false;
		}

		if (logModel == null) {
			if (log.isErrorEnabled()) {
				GameUtility.logErrorException(log, "logModel=null");
			}
			return false;
		}

		Jedis jedis = JedisManager.getResource(moduleName);

		String modelString = null;
		try {
			modelString = JsonConvert.toJson(logModel);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("convert log model " + modelString.getClass().getSimpleName()
						+ " to json fail with key " + key, e);
				return false;
			}
		}

		try {
			jedis.set(key, modelString);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("save game log to redis fail.");
				log.error(modelString);
			}
		}

		JedisManager.returnResource(moduleName, jedis);
		return true;
	}

	/**
	 * 一次刪除很多筆資料
	 * 
	 * @param keyList
	 */
	public static void deleteManyDataFromRedis(ArrayList<String> keyList) {
		if (GameUtility.getArrayListSize(keyList) == 0) {
			return;
		}

		Jedis jedis = JedisManager.getResource(moduleName);

		for (String key : keyList) {
			jedis.del(key);
		}

		JedisManager.returnResource(moduleName, jedis);
	}

	/**
	 * 刪除指定的資料
	 * 
	 * @param key
	 */
	public static void deleteDataFromRedis(String key) {
		if (key == null) {
			return;
		}

		Jedis jedis = JedisManager.getResource(moduleName);
		jedis.del(key);
		JedisManager.returnResource(moduleName, jedis);
	}

	/**
	 * 取得指定條件的 key list
	 * 
	 * @param keyPattern
	 *            "key1:key2:*", "*:key2" ....
	 * @return
	 */
	public static ArrayList<String> getKeyListFromRedis(String keyPattern) {
		Jedis jedis = JedisManager.getResource(moduleName);
		Set<String> keys = jedis.keys(keyPattern);
		JedisManager.returnResource(moduleName, jedis);

		ArrayList<String> keyList = new ArrayList<String>();
		if (keys != null) {
			keyList.addAll(keys);
		}
		return keyList;
	}

	/**
	 * 從 redis 載入指定的 log model
	 * 
	 * @param key
	 * @return
	 */
	public static BasicLogModel loadLogModelFromRedis(String key) {
		Jedis jedis = JedisManager.getResource(moduleName);
		String modelString = jedis.get(key);
		JedisManager.returnResource(moduleName, jedis);

		if (!GameUtility.isHaveString(modelString)) {
			return null;
		}

		BasicLogModel model = null;
		try {
			model = (BasicLogModel) JsonConvert.toObject(modelString);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("convert string to log model object fail.", e);
				log.error(modelString);
			}
		}

		return model;
	}
	
	/**
	 * 儲存紀錄到REDIS
	 * @param key
	 * @param field
	 * @param value
	 */
	public static void saveTempToRedis(String key, String field, String value){
		Jedis jedis = JedisManager.getResource(moduleName);
		jedis.hset(key, field, value);
		JedisManager.returnResource(moduleName, jedis);
	}
	
	/**
	 * 從REDIS取得KEY全部的紀錄
	 * @param key
	 * @return
	 */
	public static Map<String, String> getRecordFromRedis(String key){
		Map<String, String> result = null;
		Jedis jedis = JedisManager.getResource(moduleName);
		result = jedis.hgetAll(key);
		JedisManager.returnResource(moduleName, jedis);
		return result;
	}
	
	/**
	 * 刪除REDIS的紀錄
	 * @param key
	 */
	public static void deleteTempFromRedis(String key){
		Jedis jedis = JedisManager.getResource(moduleName);
		if(jedis.hgetAll(key) != null){
			jedis.del(key);
		}
		else{
			return;
		}
		JedisManager.returnResource(moduleName, jedis);
	}
}
